package com.itheima.d2_reflect;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

public class MyBeanUtils {

    public static void populate(Object bean, Map<String,Object> map) throws NoSuchFieldException, IllegalAccessException {

//        1.获取bean对象的class对象
        Class<?> clazz = bean.getClass();
//        2.获取map集合的所有Key
        Set<String> keys = map.keySet();
//        3.遍历key,通过key获取Field成员变量对象
        for (String key : keys) {
            Field field = clazz.getDeclaredField(key);
//        4.将key对应的value值设置到Field
            Object value = map.get(key);
//            开启访问权限后在赋值
            field.setAccessible(true);
            field.set(bean,value);
        }
    }
}
